using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Snow
{
    [Serializable]
    class AppSettings
    {
        private const string FILE_NAME = "config.ini";

        public int Wind;
        public int iSoLuong;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32
        uiParam, String pvParam, UInt32 fWinIni);
        private const Int32 SPI_SETDESKWALLPAPER = 20;
        private const Int32 SPIF_UPDATEINIFILE = 0x01;
        private const Int32 SPIF_SENDWININICHANGE = 0x02;
        public AppSettings()
        {
            Wind = 10;
            iSoLuong = 60;

            RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if (rk.GetValue("MVT-Snow") == null || rk.GetValue("MVT-Snow").ToString() != Application.ExecutablePath.ToString())
            {
                rk.SetValue("MVT-Snow", Application.ExecutablePath.ToString());
            }

            Bitmap bmp = new Bitmap(Properties.Resources.background);
            bmp.Save(Application.StartupPath + "\\background.jpg");
            //SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, Application.StartupPath + "\\background.jpg", SPIF_UPDATEINIFILE);
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, Application.StartupPath + "\\background.jpg", SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        }
        #region Save Load

        public static void SaveToFile(AppSettings settings)
        {
            if (settings == null)
                return;
            Stream fileStream = File.Open(FILE_NAME,
                FileMode.Create, FileAccess.ReadWrite, FileShare.None);

            BinaryFormatter binaryFormatter = new BinaryFormatter();
            try
            {
                binaryFormatter.Serialize(fileStream, settings);
            }
            catch
            {
                throw;
            }
            finally
            {
                fileStream.Flush();
                fileStream.Close();
            }
        }
        public static AppSettings LoadFromFile()
        {
            AppSettings obj;
            if (!File.Exists(FILE_NAME))
            {
                obj = new AppSettings();
                return obj;
            }
            Stream fileStream = null;

            BinaryFormatter binaryFormatter = new BinaryFormatter();

            fileStream = File.Open(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.None);
            try
            {
                obj = (AppSettings)binaryFormatter.Deserialize(fileStream);
                return obj;
            }
            catch
            {
                return new AppSettings();
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }
        }
        #endregion
    }
}
